package com.airn.siege.lion.lib.widget

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import java.util.*

/**
 * Created by airn on 2022-02-24.
 **/
class TimeTextView : AppCompatTextView {
    private val handler: TimeHandler = TimeHandler(this)

    constructor(context: Context) : this(context, null)

    constructor(context: Context, set: AttributeSet?) : this(context, set, 0)

    constructor(context: Context, set: AttributeSet?, defStyleAttr: Int) : super(context, set, defStyleAttr) {
        init()
    }


    /**
     * 获取当前时间并启动时钟更新线程
     *
     */
    private fun init() {
        updateClock()

        Thread { handler.startScheduleUpdate() }.start()
    }

    /**
     * 更新时钟
     */
    private fun updateClock() {
        val calendar = Calendar.getInstance(TimeZone.getDefault())
        val year = calendar.get(Calendar.YEAR)
        val month = calendar.get(Calendar.MONTH) + 1
        val day = calendar.get(Calendar.DAY_OF_MONTH)
        val hour = calendar.get(Calendar.HOUR_OF_DAY)
        val minute = calendar.get(Calendar.MINUTE)
        val second = calendar.get(Calendar.SECOND)
        val dayStr = formatInt(day)

        val monthStr = formatInt(month)

        val hourStr = formatInt(hour)

        val minuteStr = formatInt(minute)

        val secondStr = formatInt(second)

        val weekStr = getWeak(calendar)

        this.text = String.format("%1\$s-%2\$s-%3\$s %4\$s:%5\$s:%6\$s 星期%7\$s", year, monthStr, dayStr, hourStr, minuteStr, secondStr, weekStr)
    }

    /**
     * 获取当前星期
     * @param calendar Calendar
     * @return String
     */
    private fun getWeak(calendar: Calendar): String {
        return when (calendar.get(Calendar.WEEK_OF_MONTH)) {
            1 -> "日"
            2 -> "一"
            3 -> "二"
            4 -> "三"
            5 -> "四"
            6 -> "五"
            else -> "六"
        }
    }

    /**
     * 格式化 个位数格式
     * @param number Int
     */
    private fun formatInt(number: Int): String {
        return when (number < 10) {
            true -> "0$number"
            else -> "$number"
        }
    }

    companion object {
        private class TimeHandler(private val timeTextView: TimeTextView) : Handler(Looper.getMainLooper()) {

            private var mStopped = false

            fun post() { //每隔1秒发送一次消息
                sendMessageDelayed(obtainMessage(0), 1000)
            }

            override fun handleMessage(msg: Message) {
                super.handleMessage(msg)
                if (!mStopped) {
                    timeTextView.updateClock() //实现实时更新
                    post()
                }
            }

            fun startScheduleUpdate() {
                mStopped = false
                post()
            }

            //            fun stopScheduleUpdate() {
            //                mStopped = true
            //            }
        }
    }
}